#pragma once
#include "util.hpp"

namespace qbe::editing::Align {
void layer(AlignModel& model) {
  // selected_elements = LayoutCtrlData::getSelected_1();

  //过滤引脚
  std::vector<std::pair<entt::entity,  uint32_t>> not_include_pin_elements;
  for (auto it : model.select_arr_without_no_layer) {
    // if (it->getEleType() == ElememtType::PIN) {
    //   continue;
    // }
    not_include_pin_elements.push_back(it);
  }

  std::map<uint32_t, QImage> layer_images =  model.layer_interface.getAllLayerImageFunc();

  for (auto i : not_include_pin_elements) {
      auto rule_ptr = model.cell->reg.try_get<qlib::Ruler::s_ptr>(i.first);
      if (rule_ptr)
        continue;
    QComboBox* combox_box = new QComboBox();
    //m_align->addLayerWidget(combox_box);
    model.ele_combobox.insert({i.first, combox_box});

    auto ref_ptr = model.cell->reg.try_get<qlib::Reference::s_ptr>(i.first);
    if (ref_ptr){
     std::unordered_set<uint32_t> layer_nums;
     std::set<qlib::Tag>  ref_cell_tags = (*ref_ptr)->cell->tags;
     for(qlib::Tag i : ref_cell_tags)
     {
       layer_nums.insert(i.layer);
     }
    
    auto refs = (*ref_ptr)->cell->reg.view<qlib::Reference::s_ptr>();
      for (auto [e, cell_ref] : refs.each()) {
        std::unordered_set<uint32_t> depend_cell_layers;
        std::set<qlib::Tag>  tags = cell_ref->cell->tags;
        for(qlib::Tag i : tags)
        {
            depend_cell_layers.insert(i.layer);
        }
        for (auto iter = depend_cell_layers.begin(); iter != depend_cell_layers.end(); iter++)
          layer_nums.insert(*iter);
      }

      for (auto j : layer_nums) {
        auto iter = layer_images.find(j);
        if (iter == layer_images.end())
          continue;
        QIcon icon;
        icon.addPixmap(QPixmap::fromImage(iter->second).scaled(48, 28));
        QString str = iter->second.text();
        str += "/";
        str += QString::number(j);
        combox_box->addItem(icon, str);
      }
    } else {
      auto iter = layer_images.find(i.second);
      QIcon icon;
      icon.addPixmap(QPixmap::fromImage(iter->second).scaled(48, 28));
      QString str = iter->second.text();
      str += "/";
      str += QString::number(i.second);
      combox_box->addItem(icon, str);
    }
  }
}
}